import json
import util.ccrypto
"""
锁定脚本
"""
class CScriptPubKey:
    def __init__(self,scriptPubkey="",sigtype=1):
        #收款人的地址
        self.__scriptPubkey=scriptPubkey
        self.__sigtype = sigtype

    @property
    def scriptPubkey(self):
        return self.__scriptPubkey

    @scriptPubkey.setter
    def scriptPubkey(self,value):
        self.__scriptPubkey =value
    
    @property
    def sigtype(self):
        return self.__sigtype

    @sigtype.setter
    def sigtype(self,value):
        self.__sigtype =value
    
    #Object——>jsonStr
    def serializeToJsonStr(self):
        sttr=json.dumps(self, default=lambda obj: obj.__dict__)
        return sttr.replace("_CScriptPubKey__","")

    #jsonStr——> Object
    @classmethod
    def deserializeFromJsonStr(cls,jsonStrobjectdata):
        d = json.loads(jsonStrobjectdata)
        #print(d['txins'])
        return  cls(d['scriptPubkey'],d['sigtype'])
       
    
    #Object——>Python Object
    def serializeToPyObject(self):
        return json.loads(self.serializeToJsonStr())
    
    #Python Object——>Object
    @classmethod
    def deserializeFromPyObject(cls,pyObject):
       return cls.deserializeFromJsonStr(json.dumps(pyObject))
    def isBelongTothisAddress(self,adress):
        if self.sigtype == 1:
            if  adress == self.__scriptPubkey:
                return True
            else:
                return False
        return False

    
    def valid(self,data,scriptSig):
        '''
        @scriptSig  CScriptSig object
        '''
        if self.sigtype == 1:
            if scriptSig.scriptType==1:
                if self.scriptPubkey == util.ccrypto.CCrypto.getAddressFromPublicKey(bytes(scriptSig.pubkey, encoding = "utf8")):
                    is_verify=util.ccrypto.CCrypto.verifySignature(data,scriptSig.scriptsig,scriptSig.pubkey)
                    return is_verify
                else:
                    return False
            if scriptSig.scriptType==2:
                if self.scriptPubkey == util.ccrypto.CCrypto.getAddressFromPublicKey(bytes(scriptSig.pubkey, encoding = "utf8")):
                    is_verify=util.ccrypto.CCrypto.verifySignature(data,scriptSig.scriptsig,scriptSig.pubkey)
                    return is_verify
                else:
                    return False
            return False 
        if self.sigtype == 2:
            pass

        return False

    
"""
解锁脚本
"""
class CScriptSig:
    def __init__(self,scriptsig,pubkey,scriptType=1):
        self.__scriptsig=scriptsig
        self.__pubkey=pubkey
        self.__scriptType=scriptType
    @property
    def scriptType(self):
        return self.__scriptType

    @scriptType.setter
    def scriptType(self,value):
        self.scriptType =value
    
    @property
    def scriptsig(self):
        return self.__scriptsig

    @scriptsig.setter
    def scriptsig(self,value):
        self.scriptsig =value
    
    @property
    def pubkey(self):
        return self.__pubkey

    @pubkey.setter
    def pubkey(self,value):
        self.__pubkey =value
    #Object——>jsonStr
    def serializeToJsonStr(self):
        sttr=json.dumps(self, default=lambda obj: obj.__dict__)
        return sttr.replace("_CScriptSig__","")

    #jsonStr——> Object
    @classmethod
    def deserializeFromJsonStr(cls,jsonStrobjectdata):
        d = json.loads(jsonStrobjectdata)
        #print(d['txins'])
        return  cls(d['scriptsig'],d['pubkey'],d['scriptType'])
       
    
    #Object——>Python Object
    def serializeToPyObject(self):
        return json.loads(self.serializeToJsonStr())
    
    #Python Object——>Object
    @classmethod
    def deserializeFromPyObject(cls,pyObject):
       return cls.deserializeFromJsonStr(json.dumps(pyObject))  